package com.javen.controller;

import com.alibaba.fastjson.JSONObject;
import com.javen.model.Car;
import com.javen.model.Deal;
import com.javen.model.Equipment;
import com.javen.model.User;
import com.javen.service.ICarService;
import com.javen.service.IDealService;
import com.javen.service.IEquipmentService;
import com.javen.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping({"/equipment"})
public class EquipmentController {
    private static Logger log = LoggerFactory.getLogger(EquipmentController.class);
    @Resource
    private IEquipmentService equipmentService;
    @Resource
    private IDealService dealService;
    @Resource
    private ICarService carService;
    @Resource
    private IUserService userService;

    @RequestMapping(value = {"/showallequip"}, method = {org.springframework.web.bind.annotation.RequestMethod.GET})
    @ResponseBody
    public List<Equipment> getUserInJson2(Map<String, Object> model) {
        List<Equipment> equipment = this.equipmentService.showAllEquip();
        log.info(equipment.toString());
        return equipment;
    }

    @RequestMapping(value = {"/createequip"}, method = {org.springframework.web.bind.annotation.RequestMethod.POST})
    @ResponseBody
    public JSONObject createEquip(HttpServletRequest request, Model model) {
        String equip_id = request.getParameter("equip_id");
        String location_nl = "31.893959";
        String location_el = "120.54731";
        String description = request.getParameter("description");
        int state = 0;

        String username = request.getParameter("username");
        User user = new User();
        user = this.userService.getUserByUserName(username);
        String user_id = user.getUserId();
        System.out.println(user_id);
        Equipment equip = new Equipment();
        equip.setEquipId(equip_id);
        equip.setUserId(user_id);
        equip.setLocationNL(location_nl);
        equip.setLocationEL(location_el);
        equip.setDescription(description);
        equip.setState(Integer.valueOf(state));
        int res = 0;
        JSONObject json = new JSONObject();
        if (res == 1) {
            json.put("res", Integer.valueOf(1));
            System.out.print("success");
        } else {
            json.put("res", Integer.valueOf(0));
            System.out.println("falied");
        }
        return json;
    }


    @RequestMapping(value = {"/reserve"}, method = {org.springframework.web.bind.annotation.RequestMethod.POST})
    @ResponseBody
    public JSONObject reserve(HttpServletRequest request, Model model) {
        String username = request.getParameter("username");
        System.out.println(username);
        String equip_id = request.getParameter("equipId");
        System.out.println(equip_id);
        String licence_plate = request.getParameter("licence_plate");

        System.out.println(licence_plate);

        Double price = Double.valueOf(10.0D);

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :HH:mm:ss");
        String start_time = dateFormat.format(date).toString();

        Integer status = Integer.valueOf(1);

        Deal deal = new Deal();
        deal.setEquipId(equip_id);
        deal.setUserName(username);
        deal.setLicencePlate(licence_plate);
        deal.setPrice(price);
        deal.setStartTime(start_time);
        deal.setCompleted(status);

        int res_deal = this.dealService.insertDealRecord(deal);
        System.out.println(res_deal);
        JSONObject json = new JSONObject();
        if (res_deal != 1) {
            json.put("res", Integer.valueOf(0));
            return json;
        }


        Car car = new Car();
        car.setLicencePlate(licence_plate);
        car.setStatus(status);

        int res_car = this.carService.updateStatusByLicence(car);
        System.out.println(res_car);
        if (res_deal != 1) {
            json.put("res", Integer.valueOf(0));
            return json;
        }


        Equipment equip = new Equipment();
        int state = 1;
        equip.setState(Integer.valueOf(state));
        equip.setEquipId(equip_id);

        int res = this.equipmentService.updateByEquipId(equip);
        if (res == 1) {
            json.put("res", Integer.valueOf(1));
            log.debug("reserve success");
            System.out.println("success");
        } else {
            json.put("res", Integer.valueOf(0));
            log.debug("reserve failed");
            System.out.println("failed");
        }
        return json;
    }
}
